# Maintainer: Jeff Hubbard <musikernel@gmail.com>

_realname=rubberband
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.2
pkgrel=2
pkgdesc="High quality software library for audio time-stretching and pitch-shifting. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://breakfastquay.com/rubberband/"
license=('spdx:GPL-2.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-libsamplerate"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-ladspa-sdk"
         "${MINGW_PACKAGE_PREFIX}-lv2"
         "${MINGW_PACKAGE_PREFIX}-vamp-plugin-sdk")
source=("https://breakfastquay.com/files/releases/${_realname}-${pkgver}.tar.bz2"
        001-use-system-memory-barrier-on-mingw.patch)
sha256sums=('dda7e257b14c59a1f59c5ccc4d6f19412039f77834275955aa0ff511779b98d2'
            'c66b22f94ace3fd82c24be5ec87069f44410fa5dbd43546cd811816c324fcb8f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ../001-use-system-memory-barrier-on-mingw.patch
}
build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    --wrap-mode=nodownload \
    --default-library=both \
    --auto-features=enabled \
    -Dfft=fftw \
    -Dresampler=libsamplerate \
    -Djni=disabled \
    -Dtests=disabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe setup --reconfigure -Dtests=enabled "../${_realname}-${pkgver}"
  ${MINGW_PREFIX}/bin/meson.exe compile
  ${MINGW_PREFIX}/bin/meson.exe test || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
